package com.dianxin.smartpark.service.visitor.impl;

import java.util.Date;
import java.util.List;

import com.chinatelecom.response.common.CommonResponseBody;
import com.chinatelecom.response.constant.ErrMsgEnum;
import com.chinatelecom.response.utils.BeanUtil;
import com.chinatelecom.response.utils.ResponseBodyWrapperUtil;
import com.chinatelecom.response.utils.StringUtils;
import com.dianxin.smartpark.dao.dataobject.visitor.SmartParkStrategyConfig;
import com.dianxin.smartpark.dao.dataobject.visitor.SmartParkStrategyConfigExample;
import com.dianxin.smartpark.dao.mapper.visitor.SmartParkStrategyConfigMapper;
import com.dianxin.smartpark.dao.qo.visitor.SmartParkStrategyConfigQO;
import com.dianxin.smartpark.service.visitor.SmartParkStrategyConfigService;
import com.dianxin.smartpark.service.vo.visitor.SmartParkStrategyConfigVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 访客策略配置Service业务层处理
 * 
 * @author chenjiasong
 * @date 2021-07-15
 */
@Service
public class SmartParkStrategyConfigServiceImpl implements SmartParkStrategyConfigService
{
    private final static Logger logger = LoggerFactory.getLogger(SmartParkStrategyConfigServiceImpl.class);
    @Resource
    private SmartParkStrategyConfigMapper smartParkStrategyConfigMapper;

    /**
     * 查询访客策略配置
     * 
     * @param qo 访客策略配置ID
     * @return 访客策略配置
     */
    @Override
    public CommonResponseBody<SmartParkStrategyConfigVO> selectSmartParkStrategyConfig(SmartParkStrategyConfigQO qo)
    {
        if(StringUtils.isNull(qo.getTenantId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        SmartParkStrategyConfigExample smartParkStrategyConfigExample = new SmartParkStrategyConfigExample();
        SmartParkStrategyConfigExample.Criteria criteria = smartParkStrategyConfigExample.createCriteria();
        criteria.andTenantIdEqualTo(qo.getTenantId());
        List<SmartParkStrategyConfig> passRecords = smartParkStrategyConfigMapper.selectByExample(smartParkStrategyConfigExample);
        SmartParkStrategyConfig smartParkStrategyConfig = null;
        if(StringUtils.isEmpty(passRecords)){
            smartParkStrategyConfig = new SmartParkStrategyConfig();
            smartParkStrategyConfig.setTenantId(qo.getTenantId());
            smartParkStrategyConfig.beforeInsert();
            smartParkStrategyConfigMapper.insert(smartParkStrategyConfig);
        }else {
            smartParkStrategyConfig = passRecords.get(0);
        }
        return ResponseBodyWrapperUtil.successWrapper(smartParkStrategyConfig,SmartParkStrategyConfigVO.class);
    }

    /**
     * 查询访客策略配置列表
     * 
     * @param qo 访客策略配置
     * @return 访客策略配置
     */
    @Override
    public CommonResponseBody<List<SmartParkStrategyConfigVO>> selectSmartParkStrategyConfigList(SmartParkStrategyConfigQO qo)
    {
        SmartParkStrategyConfigExample smartParkStrategyConfigExample = new SmartParkStrategyConfigExample();
        SmartParkStrategyConfigExample.Criteria criteria = smartParkStrategyConfigExample.createCriteria();
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        criteria.andIsDeletedEqualTo(0);
        if (qo.getNeedPage()) {
            smartParkStrategyConfigExample.setOrderByClause("gmt_create desc limit " + (qo.getPageIndex() - 1) * qo.getPageSize() + "," + qo.getPageSize());
        } else {
            smartParkStrategyConfigExample.setOrderByClause("gmt_create desc");
        }

        List<SmartParkStrategyConfig> passRecords = smartParkStrategyConfigMapper.selectByExample(smartParkStrategyConfigExample);
        long count = smartParkStrategyConfigMapper.countByExample(smartParkStrategyConfigExample);

        return ResponseBodyWrapperUtil
                .successListWrapper(passRecords, count, qo, SmartParkStrategyConfigVO.class);
    }

    /**
     * 新增访客策略配置
     * 
     * @param vo 访客策略配置
     * @return 结果
     */
    @Override
    public CommonResponseBody<Boolean> insertSmartParkStrategyConfig(SmartParkStrategyConfigVO vo)
    {
        // 验证 todo
        SmartParkStrategyConfig smartParkStrategyConfig = new SmartParkStrategyConfig();
        try {
            BeanUtil.insertConversion(vo, smartParkStrategyConfig);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkStrategyConfig.beforeInsert();
        return ResponseBodyWrapperUtil.toResData(smartParkStrategyConfigMapper.insertSelective(smartParkStrategyConfig));
    }

    /**
     * 修改访客策略配置
     * 
     * @param vo 访客策略配置
     * @return 结果
     */
    @Override
    public CommonResponseBody<Boolean> updateSmartParkStrategyConfig(SmartParkStrategyConfigVO vo)
    {
        // 验证 todo
        SmartParkStrategyConfig smartParkStrategyConfig = new SmartParkStrategyConfig();
        try {
            BeanUtil.insertConversion(vo, smartParkStrategyConfig);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkStrategyConfig.beforeUpdate();
        return ResponseBodyWrapperUtil.toResData(smartParkStrategyConfigMapper.updateByPrimaryKeySelective(smartParkStrategyConfig));
    }

    /**
     * 删除访客策略配置信息
     * 
     * @param vo 访客策略配置ID
     * @return 结果
     */
    @Override
    public CommonResponseBody<Boolean> deleteSmartParkStrategyConfig(SmartParkStrategyConfigVO vo)
    {
        if(StringUtils.isNull(vo.getId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        // 验证 todo
        SmartParkStrategyConfig smartParkStrategyConfig = new SmartParkStrategyConfig();
        try {
            BeanUtil.insertConversion(vo, smartParkStrategyConfig);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkStrategyConfig.beforeDelete();
        return ResponseBodyWrapperUtil.toResData(smartParkStrategyConfigMapper.updateByPrimaryKeySelective(smartParkStrategyConfig));
    }
}
